package com.ruoyi.common.utils.ffmpeg;

import com.ruoyi.common.core.domain.entity.SysUser;
import io.swagger.models.auth.In;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName FFmpegUtil
 * @Description TODO 视频切割
 * @Author luguanqing
 * @Date 2021/4/8 9:32
 * @Version 1.0
 */
public class FFmpegUtil {

    /**
     *@param videoFile  原视频
     *@param outputFile  截取后视频
     *@param timeLength  截取时间长度，单位秒
     *切割视频
     */
    public static void cutVideo(File videoFile, File outputFile, int timeLength){
        String cmdStr;
        if(timeLength >= 300){
            timeLength = 300/60;
            cmdStr = "D:\\软件\\ffmpeg\\bin\\ffmpeg.exe -ss 0:0:0 -t" + " 0:" + timeLength + ":0" + " -i " + videoFile + " -vcodec copy -acodec copy " + outputFile;
        }else{
            int min = timeLength/60;
            int sec = timeLength - 60 * min;
            cmdStr = "D:\\软件\\ffmpeg\\bin\\ffmpeg.exe -ss 0:0:0 -t" + " 0:" + min + ":" + sec + " -i " + videoFile + " -vcodec copy -acodec copy " + outputFile;
        }
        Runtime run = Runtime.getRuntime();
        try{
            Process process = run.exec("c:/nircmd.exe elevate  " + cmdStr);
            InputStream in = process.getInputStream();
            in.close();
            process.waitFor();
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("----截取视频成功----");
    }

     /**
       * @author luguanqing
       * Description
       * @Date 10:20 2021/4/8
       * @Param video_path 视频路径
       * @return time 时长
       **/
     public static Integer getVideoTime(String video_path){
         List<String> commands = new ArrayList<String>();
         commands.add("D:\\软件\\ffmpeg\\bin\\ffmpeg.exe");
         commands.add("-i");
         commands.add(video_path);
         try {
             ProcessBuilder builder = new ProcessBuilder();
             builder.command(commands);
             final Process p = builder.start();

             //从输入流中读取视频信息
             BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
             StringBuffer sb = new StringBuffer();
             String line = "";
             while ((line = br.readLine()) != null) {
                 sb.append(line);
             }
             br.close();

             //从视频信息中解析时长
             String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
             Pattern pattern = Pattern.compile(regexDuration);
             Matcher m = pattern.matcher(sb.toString());
             if (m.find()) {
                 int time = getTimelen(m.group(1));
                 System.out.println(video_path+",视频时长："+time+", 开始时间："+m.group(2)+",比特率："+m.group(3)+"kb/s");
                 return time;
             }
         } catch (Exception e) {
             e.printStackTrace();
         }

         return 0;
     }

    /**
     * 格式化视频时长
     */
    public static int getTimelen(String timelen){
        int min = 0;
        String strs[] = timelen.split(":");
        if(strs[0].compareTo("0") >0){
            //秒
            min += Integer.valueOf(strs[0]) * 60 * 60;
        }
        if(strs[1].compareTo("0") > 0){
            min += Integer.valueOf(strs[1]) * 60;
        }
        if(strs[2].compareTo("0") > 0){
            min += Math.round(Float.valueOf(strs[2]));
        }
        return min;
    }

    public static void main(String[] args) {
//        File videoFile = new File("D:\\sparkle_your_name_am360p.mp4");
//        File outFile = new File("D:\\test.mp4");
//        cutVideo(videoFile, outFile, 59);
       // getVideoTime("D:\\ruoyi\\uploadPath\\upload\\2021\\04\\08\\Video_2021-04-02_092217.wmv");
    }
}
